<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;

class IndexResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        if ($this->template_id == 'i_5'){
            //首页加载热门资讯时一次性返回50篇
            $child = IndexContentResource::collection($this->articles->take(50));
        }else{
            $child = IndexContentResource::collection($this->articles->take(5));
        }

        return [
            'id' => $this->id,
            'order' => $this->order,
            'title' => $this->title,
            'template_id' => $this->template_id,
            'margin_top' => $this->margin_top,
            'is_show_title' => $this->is_show_title,
            'sub_title' => $this->sub_title,
            'title_icon' => config('app.url').'/uploads/'.$this->title_icon,
            'more_text' => $this->more_text,
            'more_icon' => $this->more_icon,
            'more_url' => $this->more_url,
            'child' => $child
        ];
    }
}
